package cn.itcast.oa.action;

import java.util.HashSet;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.base.ModelDrivenBaseAction;
import cn.itcast.oa.model.Department;
import cn.itcast.oa.model.Role;
import cn.itcast.oa.model.User;
import cn.itcast.oa.util.DepartmentUtils;
import cn.itcast.oa.util.HqlHelper;

@Controller
@Scope("prototype")
public class UserAction extends ModelDrivenBaseAction<User>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Long departmentId;
	private Long[] roleIds;
	
	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}

	public String add() throws Exception{
		Department department=departmentService.getById(departmentId);
		model.setDepartment(department);
		
		List<Role> roleList=roleService.getByIds(roleIds);
		model.setRoles(new HashSet<Role> (roleList));
		model.setPassword(DigestUtils.md5Hex("1234"));
		
		userService.save(model);
		return "tolist";
	}
	
	public String delete() throws Exception{
		userService.delete(model.getId());
		return "tolist";
	}
	
	public String list() throws Exception{
		//List<User> userList=userService.findAll();
		//ActionContext.getContext().put("userList", userList);
		//分页信息
		new HqlHelper(User.class)
		.buildPageBeanForStruts2(pageNum, userService);
		return "list";
	}
	
	public String edit() throws Exception{
		
		User user=userService.getById(model.getId());
	    user.setEmail(model.getEmail());
	    user.setGender(model.getGender());
	    user.setLoginName(model.getLoginName());
	    user.setName(model.getName());
	    //user.setPassword(model.getPassword());
	    user.setPhoneNumber(model.getPhoneNumber());
	    user.setDescription(model.getDescription());
	    
	    Department department=departmentService.getById(departmentId);
	    user.setDepartment(department);
	    
	    List<Role> roleList=roleService.getByIds(roleIds);
	    user.setRoles(new HashSet<Role>(roleList));
	    userService.update(user);
	   
	    return "tolist";
	}
	
	public String editUI() throws Exception{
		List<Department> topList=departmentService.findTopList();
		List<Department> departmentList=DepartmentUtils.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);
		
		List<Role> roleList=roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		
		//回显信息
		User user = userService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(user);
		if (user.getDepartment() != null) {
			departmentId = user.getDepartment().getId();
		}
		if (user.getRoles().size() > 0) {
			roleIds = new Long[user.getRoles().size()];
			int index = 0;
			for (Role role : user.getRoles()) {
				roleIds[index++] = role.getId();
			}
		}
		return "saveUI";
	}
	
	public String addUI() throws Exception{
	 List<Department> topList=departmentService.findTopList();
	 List<Department> departmentList=DepartmentUtils.getAllDepartments(topList);
	 ActionContext.getContext().put("departmentList", departmentList);
	 
	 List<Role> roleList=roleService.findAll();
	 ActionContext.getContext().put("roleList", roleList);
		return "saveUI";
	}
	
	public String initPassword() throws Exception{
		// 1，从数据库中取出原对象
				User user = userService.getById(model.getId());

				// 2，设置要修改的属性（要使用MD5摘要）
				
				user.setPassword(DigestUtils.md5Hex("1234"));

				// 3，更新到数据库
				userService.update(user);

				return "tolist";
	}
	public String loginUI()throws Exception{
		return "loginUI";
	}
	
	public String login() throws Exception{
		//model.getLoginName();
		//model.getPassword();
		User user=userService.getById(model.getLoginName(),model.getPassword());
		if(user==null&&model.getLoginName()!=null){
			addFieldError("login", "密码错误");
			return "loginUI";
		}
		else if(model.getLoginName()==null){
			addFieldError("login","请先登录");
			return "loginUI";
		}
		else{
			ActionContext.getContext().getSession().put("user", user);
			return "toIndex";
		}
	}
	
	public String loginOut() throws Exception{
		ActionContext.getContext().getSession().remove("user");
		return "loginOut";
	}

	public Long getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}
}
